package com.chezhujie.www.utils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author huangkai
 * @date 2017年6月15日 下午7:03:30
 * @version V1.0
 */
public class Award implements Serializable{
    /**编号*/
    private String id;
    /**概率（0.1代表10%，最多3位小数，即千分之一级）*/
    private double probability;
    /**数量（该类奖品剩余数量）*/
    private int count;
    /**名字*/
    private String name;
    /**奖品顺序*/
    private String ext1;
    /**奖品是否有效*/
    private String ext2;   //   0是不存在 1是存在
    /**商品Id*/
    private String ext3;   //   0是不存在 1是存在    
    /**活动id*/
    private String festivalActId;    
    /**商家id*/
    private String businessId;
    /**奖品类型*/
    private Integer discountType;  
    /**优惠价钱或者红包价钱*/
    private BigDecimal discountPrice;
    /**奖品价钱*/ //红包的时候代表大区间价钱
    private BigDecimal price; 
    /**主键*/
    private String awardonId;
    /**红包小区间价钱*/ //红包的时候代表小区间价钱
    private BigDecimal smallprice;     
    
    public BigDecimal getSmallprice() {
		return smallprice;
	}

	public void setSmallprice(BigDecimal smallprice) {
		this.smallprice = smallprice;
	}

	public Award(String id, double probability) {
        super();
        this.id = id;
        this.probability = probability;
    }

    public Award(){

    }
            
 
    
    
    
	public String getExt3() {
		return ext3;
	}

	public void setExt3(String ext3) {
		this.ext3 = ext3;
	}

	public String getAwardonId() {
		return awardonId;
	}

	public void setAwardonId(String awardonId) {
		this.awardonId = awardonId;
	}

	public String getFestivalActId() {
		return festivalActId;
	}

	public void setFestivalActId(String festivalActId) {
		this.festivalActId = festivalActId;
	}

	public String getBusinessId() {
		return businessId;
	}

	public void setBusinessId(String businessId) {
		this.businessId = businessId;
	}

	public Integer getDiscountType() {
		return discountType;
	}

	public void setDiscountType(Integer discountType) {
		this.discountType = discountType;
	}

	public BigDecimal getDiscountPrice() {
		return discountPrice;
	}

	public void setDiscountPrice(BigDecimal discountPrice) {
		this.discountPrice = discountPrice;
	}

	public BigDecimal getPrice() {
		return price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public String getExt2() {
		return ext2;
	}

	public void setExt2(String ext2) {
		this.ext2 = ext2;
	}

	public String getExt1() {
		return ext1;
	}

	public void setExt1(String ext1) {
		this.ext1 = ext1;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public double getProbability() {
		return probability;
	}

	public void setProbability(double probability) {
		this.probability = probability;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public static Award lottery(List<Award> awards){
        //总的概率区间
    	double totalPro = 0;
        //存储每个奖品新的概率区间
        List<Double> proSection = new ArrayList<Double>();
        proSection.add(0.0);
        //遍历每个奖品，设置概率区间，总的概率区间为每个概率区间的总和
        for (Award award : awards) {
            //每个概率区间为奖品概率乘以1000（把三位小数转换为整）再乘以剩余奖品数量
            totalPro += award.probability * 1000;;
            proSection.add(totalPro);
        }
        //获取总的概率区间中的随机数
        Random random = new Random();
        float randomPro = (float)random.nextInt((int)totalPro);
        //判断取到的随机数在哪个奖品的概率区间中
        for (int i = 0,size = proSection.size(); i < size; i++) {
            if(randomPro >= proSection.get(i) 
                && randomPro < proSection.get(i + 1)){
                return awards.get(i);
            }
        }
        return null;
    }
    
    public static void main(String[] args) {
        List<Award> awards = new ArrayList<Award>();
        awards.add(new Award("a1",0.1));
        awards.add(new Award("a2",0.2));
        awards.add(new Award("a3",0.3));
        awards.add(new Award("a4",0.2));
        awards.add(new Award("a5",0.2));
        int num1= 0;
        int num2= 0;
        int num3= 0;
        int num4= 0;
        int num5= 0;
        int num6 =0;
        int num7= 0;
        for (int i = 0; i < 100000; i++) {
        	num6++;
        	String str = lottery(awards).id;
          //  System.out.println("恭喜您，抽到了：" + lottery(awards).id);
            if("a1".equals(str)){
            	num1++;
            }else
            if("a2".equals(str)){
            	num2++;
            }else
            if("a3".equals(str)){
            	num3++;
            }else
            if("a4".equals(str)){
            	num4++;
            }else
            if("a5".equals(str)){
            	num5++;
            }else{
            	num7++;
            }
        }
        
        System.out.println(num1);
        System.out.println(num2);
        System.out.println(num3);
        System.out.println(num4);
        System.out.println(num5);
        System.out.println(num6);
        System.out.println(num7);
    }
    
    
}